<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>Voya Media: DataCommunication.cs Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>


</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">Voya Media
   &#160;<span id="projectnumber">1.2.255.1212</span>
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.8.0 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>Globals</span></a></li>
    </ul>
  </div>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('a00013.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">DataCommunication.cs</div>  </div>
</div><!--header-->
<div class="contents">
<a href="a00013.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 ﻿using System;
<a name="l00002"></a>00002 <span class="keyword">using</span> System.Collections.Generic;
<a name="l00003"></a>00003 <span class="keyword">using</span> System.Diagnostics;
<a name="l00004"></a>00004 <span class="keyword">using</span> System.IO;
<a name="l00005"></a>00005 <span class="keyword">using</span> System.Linq;
<a name="l00006"></a>00006 <span class="keyword">using</span> System.Net;
<a name="l00007"></a>00007 <span class="keyword">using</span> System.Reflection;
<a name="l00008"></a>00008 <span class="keyword">using</span> System.Resources;
<a name="l00009"></a>00009 <span class="keyword">using</span> System.Text;
<a name="l00010"></a>00010 <span class="keyword">using</span> System.Text.RegularExpressions;
<a name="l00011"></a>00011 
<a name="l00012"></a><a class="code" href="a00029.html">00012</a> <span class="keyword">namespace </span>Network
<a name="l00013"></a>00013 {
<a name="l00017"></a>00017     <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">class </span>DataCommunication
<a name="l00018"></a>00018     {
<a name="l00019"></a>00019         <span class="comment">// Private class member properties.</span>
<a name="l00020"></a>00020         <span class="keyword">private</span> <span class="keyword">static</span> ResourceManager resourceManager = <span class="keyword">new</span> ResourceManager(<span class="stringliteral">&quot;Network.Properties.Resources&quot;</span>, Assembly.GetExecutingAssembly());
<a name="l00021"></a>00021 
<a name="l00027"></a>00027         <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">string</span> EscapeDirectoryPathFromHttpPost(<span class="keywordtype">string</span> unescapedPath)
<a name="l00028"></a>00028         {
<a name="l00029"></a>00029             <span class="comment">// Main variables.</span>
<a name="l00030"></a>00030             <span class="keywordtype">string</span> escapedPath = <span class="stringliteral">@&quot;&quot;</span>;
<a name="l00031"></a>00031 
<a name="l00032"></a>00032             <span class="comment">// Since the HTTP Protocol replaces spaces with &quot;+&quot; instead of &quot;%2B&quot;,</span>
<a name="l00033"></a>00033             <span class="comment">// it will create problems for directory names with &quot;+&quot; in them.</span>
<a name="l00034"></a>00034             <span class="comment">// Therefore we will first replace &quot;%2B&quot; with a replacement holder called &quot;__PLUS__&quot;.</span>
<a name="l00035"></a>00035             escapedPath = Regex.Replace(unescapedPath, <span class="stringliteral">@&quot;%2B&quot;</span>, <span class="stringliteral">@&quot;__PLUS__&quot;</span>);
<a name="l00036"></a>00036 
<a name="l00037"></a>00037             <span class="comment">// Remove strange escape characters created by the HTTP Protocol and double-escape backslash characters (&quot;\&quot;).</span>
<a name="l00038"></a>00038             escapedPath = Regex.Replace(Uri.UnescapeDataString(escapedPath), <span class="stringliteral">@&quot;(\\)+&quot;</span>, <span class="stringliteral">@&quot;\\&quot;</span>);
<a name="l00039"></a>00039 
<a name="l00040"></a>00040             <span class="comment">// Replace the problematic plus characters (&quot;+&quot;) with real spaces (&quot; &quot;).</span>
<a name="l00041"></a>00041             escapedPath = Regex.Replace(escapedPath, <span class="stringliteral">@&quot;\+&quot;</span>, <span class="stringliteral">@&quot; &quot;</span>);
<a name="l00042"></a>00042 
<a name="l00043"></a>00043             <span class="comment">// Change our &quot;__PLUS__&quot; replacement holders back to real plus characters (&quot;+&quot;).</span>
<a name="l00044"></a>00044             escapedPath = Regex.Replace(escapedPath, <span class="stringliteral">@&quot;__PLUS__&quot;</span>, <span class="stringliteral">@&quot;+&quot;</span>);
<a name="l00045"></a>00045 
<a name="l00046"></a>00046             <span class="keywordflow">return</span> escapedPath;
<a name="l00047"></a>00047         }
<a name="l00048"></a>00048 
<a name="l00054"></a>00054         <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">string</span> GetHttpPostData()
<a name="l00055"></a>00055         {
<a name="l00056"></a>00056             <span class="comment">// Main variables.</span>
<a name="l00057"></a>00057             <span class="keywordtype">string</span> postData = <span class="stringliteral">&quot;&quot;</span>;
<a name="l00058"></a>00058 
<a name="l00059"></a>00059             <span class="comment">// Check if this application was accessed using the POST HTTP Request Method.</span>
<a name="l00060"></a>00060             <span class="keywordflow">if</span> (System.Environment.GetEnvironmentVariable(<span class="stringliteral">@&quot;REQUEST_METHOD&quot;</span>) == <span class="stringliteral">@&quot;POST&quot;</span>)
<a name="l00061"></a>00061             {
<a name="l00062"></a>00062                 <span class="comment">// Get the length/size of the POST Data.</span>
<a name="l00063"></a>00063                 <span class="keywordtype">int</span> PostDataLength = Convert.ToInt32(System.Environment.GetEnvironmentVariable(<span class="stringliteral">@&quot;CONTENT_LENGTH&quot;</span>));
<a name="l00064"></a>00064 
<a name="l00065"></a>00065                 <span class="comment">// Set the Max length/size for POST Data to 2 KB.</span>
<a name="l00066"></a>00066                 <span class="keywordflow">if</span> (PostDataLength &gt; 2048)
<a name="l00067"></a>00067                 {
<a name="l00068"></a>00068                     PostDataLength = 2048;
<a name="l00069"></a>00069                 }
<a name="l00070"></a>00070 
<a name="l00071"></a>00071                 <span class="comment">// Read the POST Data one character at a time into the postData string.</span>
<a name="l00072"></a>00072                 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> dataIterator = 0; dataIterator &lt; PostDataLength; dataIterator++)
<a name="l00073"></a>00073                 {
<a name="l00074"></a>00074                     postData += Convert.ToChar(Console.Read()).ToString();
<a name="l00075"></a>00075                 }
<a name="l00076"></a>00076             }
<a name="l00077"></a>00077 
<a name="l00078"></a>00078             <span class="comment">// Return the HTTP POST data.</span>
<a name="l00079"></a>00079             <span class="keywordflow">return</span> postData;
<a name="l00080"></a>00080         }
<a name="l00081"></a>00081 
<a name="l00087"></a>00087         <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">string</span> GetHttpWebRequestResponse(<span class="keywordtype">string</span> url)
<a name="l00088"></a>00088         {
<a name="l00089"></a>00089             <span class="comment">// Main variables.</span>
<a name="l00090"></a>00090             <span class="keywordtype">string</span>          httpResponseDataString = <span class="stringliteral">@&quot;&quot;</span>;
<a name="l00091"></a>00091             HttpWebRequest  httpRequest            = null;
<a name="l00092"></a>00092             HttpWebResponse httpResponse           = null;
<a name="l00093"></a>00093             StreamReader    httpResponseData       = null;
<a name="l00094"></a>00094             
<a name="l00095"></a>00095             <span class="comment">// Create a Windows Event Log Source.</span>
<a name="l00096"></a>00096             <span class="keywordflow">if</span> (!EventLog.SourceExists(resourceManager.GetString(<span class="stringliteral">&quot;application_title&quot;</span>)))
<a name="l00097"></a>00097             {
<a name="l00098"></a>00098                 EventLog.CreateEventSource(resourceManager.GetString(<span class="stringliteral">&quot;application_title&quot;</span>), <span class="stringliteral">&quot;Application&quot;</span>);
<a name="l00099"></a>00099             }
<a name="l00100"></a>00100 
<a name="l00101"></a>00101             <span class="comment">// Get the HTTP Web Response of the request.</span>
<a name="l00102"></a>00102             <span class="keywordflow">if</span> (!String.IsNullOrEmpty(url))
<a name="l00103"></a>00103             {
<a name="l00104"></a>00104                 <span class="keywordflow">try</span>
<a name="l00105"></a>00105                 {
<a name="l00106"></a>00106                     <span class="comment">// Query the specified URL by sending an HTTP GET Request.</span>
<a name="l00107"></a>00107                     httpRequest             = HttpWebRequest.Create(url) as HttpWebRequest;
<a name="l00108"></a>00108                     httpRequest.ContentType = <span class="stringliteral">&quot;text/xml; charset=utf-8&quot;</span>;
<a name="l00109"></a>00109                     httpRequest.Method      = <span class="stringliteral">&quot;GET&quot;</span>;
<a name="l00110"></a>00110                 }
<a name="l00111"></a>00111                 <span class="keywordflow">catch</span> (Exception exception)
<a name="l00112"></a>00112                 {
<a name="l00113"></a>00113                     EventLog.WriteEntry(resourceManager.GetString(<span class="stringliteral">&quot;application_title&quot;</span>),
<a name="l00114"></a>00114                         <span class="stringliteral">@&quot;</span>
<a name="l00115"></a>00115 <span class="stringliteral">                            Class:  DataCommunication</span>
<a name="l00116"></a>00116 <span class="stringliteral">                            Method: GetHttpWebRequestResponse(&quot;&quot;&quot;</span> + url + <span class="stringliteral">@&quot;&quot;&quot;)</span>
<a name="l00117"></a>00117 <span class="stringliteral">                            Action: HttpWebRequest.Create(&quot;&quot;&quot;</span> + url + <span class="stringliteral">@&quot;&quot;&quot;)</span>
<a name="l00118"></a>00118 <span class="stringliteral">                            </span>
<a name="l00119"></a>00119 <span class="stringliteral">                            &quot;</span> + exception.Message
<a name="l00120"></a>00120                         , EventLogEntryType.Error, 123);
<a name="l00121"></a>00121                 }
<a name="l00122"></a>00122                 
<a name="l00123"></a>00123                 <span class="keywordflow">try</span>
<a name="l00124"></a>00124                 {
<a name="l00125"></a>00125                     httpResponse = httpRequest.GetResponse() as HttpWebResponse;
<a name="l00126"></a>00126                 }
<a name="l00127"></a>00127                 <span class="keywordflow">catch</span> (Exception exception)
<a name="l00128"></a>00128                 {
<a name="l00129"></a>00129                     EventLog.WriteEntry(resourceManager.GetString(<span class="stringliteral">&quot;application_title&quot;</span>),
<a name="l00130"></a>00130                         <span class="stringliteral">@&quot;</span>
<a name="l00131"></a>00131 <span class="stringliteral">                            Class:  DataCommunication</span>
<a name="l00132"></a>00132 <span class="stringliteral">                            Method: GetHttpWebRequestResponse(&quot;&quot;&quot;</span> + url + <span class="stringliteral">@&quot;&quot;&quot;)</span>
<a name="l00133"></a>00133 <span class="stringliteral">                            Action: httpRequest.GetResponse()</span>
<a name="l00134"></a>00134 <span class="stringliteral">                            </span>
<a name="l00135"></a>00135 <span class="stringliteral">                            &quot;</span> + exception.Message
<a name="l00136"></a>00136                         , EventLogEntryType.Error, 123);
<a name="l00137"></a>00137                 }
<a name="l00138"></a>00138             }
<a name="l00139"></a>00139 
<a name="l00140"></a>00140             <span class="keywordflow">try</span>
<a name="l00141"></a>00141             {
<a name="l00142"></a>00142                 <span class="comment">// Read the HTTP web response into a stream reader.</span>
<a name="l00143"></a>00143                 <span class="keywordflow">if</span> (httpResponse != null)
<a name="l00144"></a>00144                 {
<a name="l00145"></a>00145                     httpResponseData = <span class="keyword">new</span> StreamReader(httpResponse.GetResponseStream(), <span class="keyword">true</span>);
<a name="l00146"></a>00146                 }
<a name="l00147"></a>00147             }
<a name="l00148"></a>00148             <span class="keywordflow">catch</span> (Exception exception)
<a name="l00149"></a>00149             {
<a name="l00150"></a>00150                 EventLog.WriteEntry(resourceManager.GetString(<span class="stringliteral">&quot;application_title&quot;</span>),
<a name="l00151"></a>00151                     <span class="stringliteral">@&quot;</span>
<a name="l00152"></a>00152 <span class="stringliteral">                            Class:  DataCommunication</span>
<a name="l00153"></a>00153 <span class="stringliteral">                            Method: GetHttpWebRequestResponse(&quot;&quot;&quot;</span> + url + <span class="stringliteral">@&quot;&quot;&quot;)</span>
<a name="l00154"></a>00154 <span class="stringliteral">                            Action: new StreamReader(httpResponse.GetResponseStream(), true)</span>
<a name="l00155"></a>00155 <span class="stringliteral">                            </span>
<a name="l00156"></a>00156 <span class="stringliteral">                            &quot;</span> + exception.Message
<a name="l00157"></a>00157                     , EventLogEntryType.Error, 123);
<a name="l00158"></a>00158             }
<a name="l00159"></a>00159 
<a name="l00160"></a>00160             <span class="keywordflow">try</span>
<a name="l00161"></a>00161             {
<a name="l00162"></a>00162                 <span class="comment">// Read the stream reader stream into a string.</span>
<a name="l00163"></a>00163                 <span class="keywordflow">if</span> (httpResponseData != null)
<a name="l00164"></a>00164                 {
<a name="l00165"></a>00165                     httpResponseDataString = httpResponseData.ReadToEnd();
<a name="l00166"></a>00166                 }
<a name="l00167"></a>00167             }
<a name="l00168"></a>00168             <span class="keywordflow">catch</span> (Exception exception)
<a name="l00169"></a>00169             {
<a name="l00170"></a>00170                 EventLog.WriteEntry(resourceManager.GetString(<span class="stringliteral">&quot;application_title&quot;</span>),
<a name="l00171"></a>00171                     <span class="stringliteral">@&quot;</span>
<a name="l00172"></a>00172 <span class="stringliteral">                            Class:  DataCommunication</span>
<a name="l00173"></a>00173 <span class="stringliteral">                            Method: GetHttpWebRequestResponse(&quot;&quot;&quot;</span> + url + <span class="stringliteral">@&quot;&quot;&quot;)</span>
<a name="l00174"></a>00174 <span class="stringliteral">                            Action: httpResponseData.ReadToEnd()</span>
<a name="l00175"></a>00175 <span class="stringliteral">                            </span>
<a name="l00176"></a>00176 <span class="stringliteral">                            &quot;</span> + exception.Message
<a name="l00177"></a>00177                     , EventLogEntryType.Error, 123);
<a name="l00178"></a>00178             }
<a name="l00179"></a>00179 
<a name="l00180"></a>00180             <span class="comment">// Close and Dispose of open resources.</span>
<a name="l00181"></a>00181             httpResponseData.Close();
<a name="l00182"></a>00182             httpResponse.Close();
<a name="l00183"></a>00183 
<a name="l00184"></a>00184             <span class="comment">// Return the web request response as a string.</span>
<a name="l00185"></a>00185             <span class="keywordflow">return</span> httpResponseDataString;
<a name="l00186"></a>00186         }
<a name="l00187"></a>00187 
<a name="l00188"></a>00188     }
<a name="l00189"></a>00189 }
</pre></div></div><!-- contents -->
</div>
  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><a class="el" href="a00013.html">DataCommunication.cs</a>      </li>

    <li class="footer">Generated on Thu Jan 10 2013 07:05:17 for Voya Media by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.0 </li>
   </ul>
 </div>


</body>
</html>
